package com.lmpop.createIndex.search;

import com.lmpop.createIndex.searchModel.Queue;
import com.lmpop.createIndex.searchModel.QueueValue;
import com.lmpop.createIndex.searchModel.SearchData;
import com.lmpop.createIndex.searchModel.SearchModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class BrandIndexFiles
{
  public ProperUtil properUtil;
  private String file_path;

  public BrandIndexFiles()
  {
    this.properUtil = new ProperUtil();
    this.file_path = this.properUtil.getFilePathByModel("brand");
  }

  public List getGoodsBrandListNum(String cat_id_para, List goodsList) throws IOException {
    List numList = new ArrayList();
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();

    paraList.add(new SearchModel("0", "class_id_group", cat_id_para));
    ArrayList list = search.getSearchInfo(this.file_path, paraList);
    if ((list != null) && (list.size() > 0)) {
      Hashtable catMap = new Hashtable();
      for (int i = 0; i < list.size(); i++) {
        catMap = (Hashtable)list.get(i);
        String info_id = ""; String title = "";
        if (catMap.get("info_id") != null) info_id = catMap.get("info_id").toString();
        if (catMap.get("title") != null) title = catMap.get("title").toString();
        int brand_num = catEixstNum(info_id, goodsList);
        if (brand_num > 0) {
          Hashtable tempMap = new Hashtable();
          tempMap.put("brand_num", String.valueOf(brand_num));
          tempMap.put("brand_id", info_id);
          tempMap.put("brand_name", title);
          numList.add(tempMap);
        }
      }
    }
    return numList;
  }

  public int catEixstNum(String info_id, List goodsList) {
    int num = 0;
    if ((goodsList != null) && (goodsList.size() > 0)) {
      Hashtable gMap = new Hashtable();
      for (int i = 0; i < goodsList.size(); i++) {
        gMap = (Hashtable)goodsList.get(i);
        String brand_id = "";
        if (gMap.get("brand_id") != null) brand_id = gMap.get("brand_id").toString();
        if (!brand_id.equals(info_id)) continue; num++;
      }
    }
    return num;
  }

  public String getBrandNameById(String brand_id) throws IOException {
    if (brand_id.equals("")) return "";
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "info_id", brand_id));
    ArrayList list = search.getSearchInfo(this.file_path, paraList);
    String brand_name = "";
    if ((list != null) && (list.size() > 0)) {
      Hashtable map = (Hashtable)list.get(0);
      if (map.get("title") != null) brand_name = map.get("title").toString();
    }
    return brand_name;
  }

  public List getBrandList(Map paraMap) throws IOException
  {
    ArrayList showList = new ArrayList();
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();

    String title = ""; String class_id_group = ""; String content = ""; String site_url = ""; String in_date = ""; String keyword = ""; String pyKey = "A"; String cust_id = "";
    if (paraMap.get("title") != null) title = paraMap.get("title").toString();
    if (paraMap.get("cat_attr") != null) class_id_group = paraMap.get("cat_attr").toString();
    if (paraMap.get("pyKey") != null) pyKey = paraMap.get("pyKey").toString();
    if (paraMap.get("cust_id") != null) cust_id = paraMap.get("cust_id").toString();

    Queue queue = new Queue(new QueueValue[] { new QueueValue("in_date", 2, true) });

    showList = search.getSearchInfo(this.file_path, paraList, queue);
    showList = filterpyKeyList(pyKey, cust_id, showList);
    return showList;
  }

  public ArrayList filterpyKeyList(String pyKey, String cust_id, ArrayList infoList)
  {
    if (infoList == null) return null;
    if (pyKey.equals("")) return infoList;
    ArrayList thisList = new ArrayList();
    Hashtable sMap = new Hashtable();
    for (int i = 0; i < infoList.size(); i++) {
      sMap = (Hashtable)infoList.get(i);
      String key_desc = ""; String cust_id_s = "";
      if (sMap.get("pykey") != null) key_desc = sMap.get("pykey").toString().trim();
      if (sMap.get("cust_id") != null) cust_id_s = sMap.get("cust_id").toString().trim();
      if ((!key_desc.equals("")) && (key_desc.equals(pyKey)) && (cust_id_s.equals(cust_id))) {
        thisList.add(sMap);
      }
    }
    if (infoList != null) infoList.clear();
    infoList = null;
    return thisList;
  }
  public ArrayList getbrandInfo(String cat_id, String area_code, String keyword, String is_show, String search_date) throws IOException {
    ArrayList showList = new ArrayList();
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();

    Queue queue = new Queue(new QueueValue[] { new QueueValue("is_show", 2, true), new QueueValue("info_id", 2, true) });
    paraList.add(new SearchModel("0", "class_id_group", cat_id));
    paraList.add(new SearchModel("1", "in_date", search_date));
    paraList.add(new SearchModel("0", "area_attr", area_code));
    paraList.add(new SearchModel("2", keyword, new String[] { "title", "content" }));
    showList = search.getSearchInfo(this.file_path, paraList, queue);
    return showList;
  }
}